<?php

namespace App\Console\Commands;

use App\Events\PostSaved;
use App\User;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;


class InviteNew extends Command
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'invite_new';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';


    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }


    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $inviteCode = 'ZCBBH';
        for ($i = 0; $i <= 4; $i++) {
            $this->register($inviteCode,$i);
            sleep(rand(1,1));
        }


    }

    function register($inviteCode,$i)
    {
        // 定义邮箱的一部分，如域名
        $domains = [
            'gmail.com',
            'yahoo.com',
            'hotmail.com',
            'qq.com',
            '163.com',
            'outlook.com',
            'icloud.com',
        ];

// 生成随机的用户名部分。这里使用uniqid()函数生成唯一ID，
// md5()确保它是字符串，substr()取部分保持邮箱简洁
        $username = substr(md5(uniqid(rand())), 0, 8);

// 从域名数组中随机选择一个
        $domain = $domains[array_rand($domains)];

// 组合生成邮箱
        $email = $username . '@' . $domain;


        $pwd = uniqid();


        $timeS = time() * 1000;
        $device = md5(uniqid());



        $url = "http://114.132.99.21:80/api/v1/user/regiest"; // 目标URL
//        $postData = "email=$email&password=$pwd&cpassword=$pwd&inviteCode=$inviteCode&sk=&ts=$timeS&sid=CegDDgZLLYXVRWuS&av=49&wv=1362&pv=5.9.11&avn=1.2.6&sv=34&pl=2&d=OP5943L1&m=PJA110&did=1c{$rand}c{$rand2}d103aa3d9bf7c492ad41T&cid=1000";
        // 424fa270-2ff3-4e06-a85c-ccc070dd6cc9
        $postData = "email=$email&password=$pwd&cpassword=$pwd&inviteCode=$inviteCode&sk=&ts=$timeS&sid=XlnKAtdj42YLpL3E&av=49&wv=1362&pv=5.9.11&avn=2.2.3&sv=30&pl=2&d=raphael&m=Redmi%20K20%20Pro&did=$device&cid=1000";

// 初始化cURL会话
        $ch = curl_init($url);

// 设置cURL选项
        curl_setopt($ch, CURLOPT_POST, true); // 发起一个POST请求
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); // 设置POST请求的参数
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 将返回结果作为字符串返回而不是直接输出
        // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

// 设置请求头
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            "Host: 114.132.99.21",
            "Connection: keep-alive",
            "Content-Length: " . strlen($postData),
            "Accept: */*",
            "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) FLASH-CAT/1.2.6 Chrome/108.0.5359.215 Electron/22.3.5 Safari/537.36",
            "Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
            "X-Requested-With: com.flashcat.vpn",
            "Accept-Encoding: gzip, deflate",
            "Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7"
        ]);

// 发送请求并获取响应
        $response = curl_exec($ch);

// 检查是否有错误发生
//        if (curl_errno($ch)) {
//            echo 'Request Error:' . curl_error($ch);
//        } else {
//            // 打印响应内容
//            echo $response;
//        }

// 关闭cURL会话
        curl_close($ch);

        // 解压数据
        $decodedData = gzdecode($response);

        if ($decodedData === false) {
            echo "Failed to decode gzip data.";
        } else {
            dump($decodedData);
        }

    }
}

